layui.use(['form'], function () {
  var form = layui.form;
  var layer = layui.layer;

  form.verify({
    required: function (value) {
      if (!value) {
        return '该项为必填项';
      }
    }
  });

  form.on('submit(register)', function (data) {
    if (password !== rpassword) {
      layer.msg('两次密码不一致', { icon: 0 })
      return false;
    }

    axios.post('http://localhost:9000/users/register', data.field)
      .then(function (response) {
        if (response.data.code === 1) {
          layer.msg(response.data.message, { icon: 1 });
          location.href = './login.html'
        } else {
          layer.msg(response.data.message, { icon: 0 })
        }
      })
      .catch(function (error) {
        layer.msg('请求出错');
      });

    return false;
  });
});